define(["angularAMD"], function (angularAMD) {
    angularAMD.service('B0228Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog){
            this.init = function (options) {
                function openDialog(startingCheck) {
                    $mdDialog.dialog(
                        'module/moduleB/program/B02/B0228/B0228.html',
                        function(dialog){
                            var tempDialog = {
                                css_url:'module/moduleB/program/B02/B0228/B0228.css',
                                product_no: startingCheck.product_no,
                                area_display: startingCheck.equipment_no + '（' + startingCheck.equipment_name + '）',//產線
                                product_display: startingCheck.product_no + '（' + startingCheck.product_name + '）', //產品
                                startingCheckList: startingCheck.starting_checklist_get_detail, //點檢項目
                                itemStatus : function(item){
                                     if (item.inspection_type == '1') {
                                        var isInterval = true;
                                        if(item.range_min !== null){
                                            isInterval = isInterval && ((+item.range_min) <= (+item.input_value));
                                        }
                                        if(item.range_max !== null){
                                            isInterval = isInterval && ((+item.input_value) <= (+item.range_max));
                                        }

                                        if(isInterval){
                                            item.check_result = '0' //ok 0
                                            return 'check-point-ok';
                                        } else {
                                            item.check_result = '1' //ng 1
                                            return 'check-point-notok';
                                        }
                                    }
                                    return '';
                                },
                                type3Init : function(item){
                                    if (!item.input_value) {
                                        item.check_result = '1';
                                        item.input_value = 2;
                                    }
                                },
                                itemClick : function(detail){
                                    //item.isClick = !item.isClick;
                                    if (detail.input_value == '1') {
                                        detail.check_result = '1' ; //ng 1
                                        detail.input_value = 2;
                                    } else {
                                        detail.check_result = '0'; //ok 0
                                        detail.input_value = 1;
                                    }
                                },
                                back: function () {
                                    dialog.hide();
                                },
                                confirm: function () {
                                    var msg = '',
                                        content = {
                                            area_no: options.areaNo,
                                            product_no: dialog.product_no,
                                            status:'2',//2 表示全部通過、1表示有沒有通過的項目
                                            starting_checklist_get_detail: []
                                        };
                                    dialog.startingCheckList.forEach(function (item) {
                                        if (item.input_value == undefined || item.input_value === '') {
                                            // 20230325 modify by Alan for#0130601 : 修正語系
                                            msg += $filter('translate')('B0228.msg.check_value_empty', item)+'<br>';
                                        } else {
                                            if (item.check_result == '1') {
                                                content.status = '1';
                                            }

                                            var detail = {
                                                inspection_no: item.inspection_no,
                                                position_no: item.position_no,
                                                inspection_seq: item.inspection_seq,
                                                inspection_name: item.inspection_name,
                                                inspection_type: item.inspection_type,
                                                //std_value: item.std_value,
                                                range_min: item.range_min,
                                                range_max: item.range_max,
                                                inspection_method: item.inspection_method,
                                                inspection_standard: item.inspection_standard,
                                                input_value: item.input_value || '',
                                                check_result:item.check_result||''
                                            };
                                            content.starting_checklist_get_detail.push(detail);
                                        }
                                    });
                                    if(msg == ''){
                                        $MMWService.sendToServer({
                                            uri: 'ServicesSTD.Module_WIP.starting_check_list_process',
                                            content : content,
                                            success : function(data){
                                                $rootScope.showToast($filter('translate')('B0228.msg.success'));
                                                dialog.hide();
                                                if(options.feedback)
                                                    options.feedback();
                                            }
                                        });
                                    } else {
                                        $rootScope.showAlert(msg);
                                    }
                                },
                                ReleaseConfirm: function () {
                                    if (dialog.userInfo.UserNo == '' || dialog.userInfo.password=='') {
                                        $rootScope.showAlert($filter('translate')('B0228.msg.user_info_empty'));
                                    } else {
                                        var content = {
                                            userno: dialog.userInfo.UserNo,
                                            password: dialog.userInfo.password,
                                            areano: options.areaNo
                                        }
                                        $MMWService.sendToServer({
                                            uri: 'ServicesSTD.Module_WIP.starting_user_info_check',
                                            content: content,
                                            success: function (data) {
                                                dialog.mode = '';
                                                dialog.userInfo = undefined;
                                            }
                                        });
                                    }
                                }
                            };
                            if(options.type == 'release'){
                                tempDialog.mode = 'user-mode';
                                tempDialog.title = 'B0228.release.title';
                                tempDialog.userInfo = {UserNo:'',password:''};
                            } else {
                                tempDialog.mode = '';
                                tempDialog.title = 'B0228.title';
                            }
                            return tempDialog;
                        }
                    );
                }
                // 取得點檢表
                (function getCheckList(){
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_WIP.starting_checklist_info_get',
                        content : {
                            area_no: options.areaNo, //生產線/區域
                            plot_no: options.plotItem.plot_no, //生產批
                            position_no:options.position_no||''//工位
                        },
                        success : function(data){
                            openDialog(data);
                        }
                    });
                })();
            };
        }]);
});
